/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.projects.toolbox_basics_project.web.context.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.Authentication;
import org.springframework.security.AuthenticationTrustResolver;
import org.springframework.security.AuthenticationTrustResolverImpl;
import org.springframework.security.context.SecurityContextHolder;

import com.genia.toolbox.persistence.bean.CrossSessionObject;
import com.genia.toolbox.persistence.exception.PersistenceException;
import com.genia.toolbox.projects.toolbox_basics_project.security.manager.impl.TrampolineUserDetails;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.User;
import com.genia.toolbox.projects.toolbox_basics_project.spring.manager.UserManager;
import com.genia.toolbox.projects.toolbox_basics_project.web.context.UserWebContext;

/**
 * implementation of {@link SbpUserWebContext}.
 */
public class UserWebContextImpl
    implements UserWebContext
{

  /**
   * the <code>Log</code> variabe.
   */
  private static final Logger LOGGER = LoggerFactory.getLogger(UserWebContextImpl.class);

  /**
   * {@link AuthenticationTrustResolver} to decide if the user is anonymous or
   * not.
   */
  private AuthenticationTrustResolver authenticationTrustResolver = new AuthenticationTrustResolverImpl();

  /**
   * the {@link UserManager} to use.
   */
  private UserManager userManager;



  /**
   * return the user set in the context object.
   * 
   * @return the user set in the context object.
   */
  public User getCurrentUser()
  {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null || authenticationTrustResolver.isAnonymous(authentication)) {
      return null;
    }
    if (authentication.getPrincipal() instanceof TrampolineUserDetails) {
      return (User) ((CrossSessionObject) (((TrampolineUserDetails) authentication.getPrincipal()).getBaseUser())).getCurrentObject();
    }
    try {
      return getUserManager().getUser(authentication.getName().toString());
    }
    catch (PersistenceException e) {
      LOGGER.error(e.getMessage(), e);
    }
    return null;
  }



  /**
   * getter for the authenticationTrustResolver property.
   * 
   * @return the authenticationTrustResolver
   */
  public AuthenticationTrustResolver getAuthenticationTrustResolver()
  {
    return authenticationTrustResolver;
  }



  /**
   * setter for the authenticationTrustResolver property.
   * 
   * @param authenticationTrustResolver
   *          the authenticationTrustResolver to set
   */
  public void setAuthenticationTrustResolver(AuthenticationTrustResolver authenticationTrustResolver)
  {
    this.authenticationTrustResolver = authenticationTrustResolver;
  }



  /**
   * getter for the userManager property.
   * 
   * @return the userManager
   */
  public UserManager getUserManager()
  {
    return userManager;
  }



  /**
   * setter for the userManager property.
   * 
   * @param userManager
   *          the userManager to set
   */
  public void setUserManager(UserManager userManager)
  {
    this.userManager = userManager;
  }
}
